package com.woniuxy.controller;

import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.RbacManagerDTO;
import com.woniuxy.entity.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 罗蔚
 * @Date 2022/3/31 12:09
 * @Description TODO
 */
@Controller
@RequestMapping("/rbacManager")
public class RbacManagerController {
    @RequestMapping("/receiveJson")
    public ModelAndView receiveJson(@RequestBody RbacManagerDTO rbacManager){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",rbacManager);
        modelAndView.setViewName("/index.jsp");
        return modelAndView;
    }

    @RequestMapping("/comeForm")
    public ModelAndView comeForm(String name){
        //接收表单数据, 返回
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",name);
        //配置了视图解析器
        modelAndView.setViewName("success");
        return modelAndView;
    }

    //使用String跳转到success.jsp页面
    @RequestMapping("/comeFormReturnString")
    public String comeFormReturnString(ModelMap modelMap, String name){
        //方案一
        //model.addAttribute("msg",name);
        //方案二  二选一即可！
        modelMap.addAttribute("msg",name);
        return "success";
    }

    //得到json, 返回json
    @RequestMapping("/receiveJsonReturnJson")
    @ResponseBody
//    public ResponseEntity  receiveJsonReturnJson(@RequestBody RbacManagerDTO rbacManagerDTO){
//        return new ResponseEntity<RbacManagerDTO>(200, "成功", rbacManagerDTO);
//    }
    public ResponseEntity receiveJsonReturnJson(@RequestBody @Validated RbacManagerDTO rbacManager, BindingResult bindingResult){
//       手动对 校验消息的封装！
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String defaultMessage = fieldError.getDefaultMessage();
            return new ResponseEntity<>(500,defaultMessage,null);
        }else{
            ResponseEntity<RbacManagerDTO> rbacManagerDTOResponseEntity = new ResponseEntity<>(200,"成功",rbacManager);
            return rbacManagerDTOResponseEntity;
        }
    }


    //重定向方式1
//    @RequestMapping("testRedirect")
//    public String testRedirect(HttpServletResponse response) throws Exception {
//        //方式1 使用servlet的模式来重定向
//        response.sendRedirect("/rbacManager/comeFormReturnString?name=123");
//        return null;
//    }
    //重定向方式2
    @RequestMapping("testRedirect")
    public String testRedirect() throws Exception {
        //方式2 直接return重定向的路径
        return "redirect:/rbacManager/comeFormReturnString?name=333";
    }

    //转发1
//    @RequestMapping("/testForword")
//    public String testForword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.getRequestDispatcher("/rbacManager/comeFormReturnString?name=123456").forward(request, response);
//        return  null;
//    }
    //转发2
    @RequestMapping("/testForword")
    public String testForword() throws ServletException, IOException {
        return  "forward:/rbacManager/comeFormReturnString?name=111";
    }

}
